package com.tracesystem.util;

import com.tracesystem.controller.info.LoginUserInfo;
import com.tracesystem.controller.info.UserInfo;
import org.springframework.security.core.context.SecurityContextHolder;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * <p>
 * 全局工具类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
public class CommonsUtils {

    /*
     * @Description: 获取当前登录用户信息
     * @Author: JMOCOK
     * @param null:
     * @return: 登录用户参数
     **/
    public static LoginUserInfo getLoginUserInfo(){
        UserInfo userdetailsInfo = (UserInfo)(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        return WoniuCopyUtil.copyOne(userdetailsInfo, LoginUserInfo.class);
    }
    /*
     * @Description:Date转LocalDateTime
     * @Author: JMOCOK
     * @param date: 日期对象
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }
    /*
     * @Description:Date转LocalDate
     * @Author: JMOCOK
     * @param date: 日期对象
     * @return: java.time.LocalDate
     **/
    public static LocalDate dateToLocalDate(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }
}
